#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\time\schedule.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

#undef HANDLE
#undef ABSTRACT
#define STRUCT
namespace Cephei { namespace QL { namespace Times {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ISchedule
	public ref class CSchedule  : 
            public Cephei::QL::Times::ISchedule
	{
	protected: 
		boost::shared_ptr<QuantLib::Schedule>* _ppSchedule;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Schedule>* _phSchedule;
#endif
		Object^ _ScheduleOwner;     // reference to object that manages the storage for this object
	internal:
		CSchedule ();
		CSchedule (Cephei::Core::IVector<DateTime>^ dates, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::ICalendar^>^ calendar, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ convention);
		CSchedule (DateTime effectiveDate, DateTime terminationDate, Cephei::QL::Times::IPeriod^ tenor, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, QL::Times::BusinessDayConventionEnum terminationDateConvention, QL::Times::DateGeneration::RuleEnum rule, Boolean endOfMonth, Microsoft::FSharp::Core::FSharpOption<DateTime>^ firstDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ nextToLastDate);
        CSchedule (boost::shared_ptr<QuantLib::Schedule>& childNative, Object^ owner);
        CSchedule (QuantLib::Schedule& childNative, Object^ owner);
        CSchedule (CSchedule^ copy);
        CSchedule (PLATFORM::Type^ t);
#ifdef STRUCT
        CSchedule (QuantLib::Schedule childNative);
#endif       
#ifdef HANDLE
		CSchedule (QuantLib::Handle<QuantLib::Schedule>& childNative, Object^ owner);
		CSchedule (QuantLib::Handle<QuantLib::Schedule> childNative);
#endif
		virtual ~CSchedule ();
		!CSchedule ();

	internal:
		QuantLib::Schedule& GetReference ();
		boost::shared_ptr<QuantLib::Schedule>& GetShared ();
		QuantLib::Schedule* GetPointer ();
        void SetSchedule (boost::shared_ptr<QuantLib::Schedule> native)
        {
            if (_ppSchedule != NULL)
                delete _ppSchedule;
            _ppSchedule = new boost::shared_ptr<QuantLib::Schedule> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Schedule>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
		virtual DateTime At (UInt64 i) ;
        property QL::Times::BusinessDayConventionEnum BusinessDayConvention 
        {
		    virtual QL::Times::BusinessDayConventionEnum get () ;
        }
        property Cephei::QL::Times::ICalendar^ Calendar 
        {
		    virtual Cephei::QL::Times::ICalendar^ get () ;
        }
		virtual DateTime Date (UInt64 i) ;
        property Cephei::Core::IVector<DateTime>^ Dates 
        {
		    virtual Cephei::Core::IVector<DateTime>^ get () ;
        }
        property Boolean Empty 
        {
		    virtual Boolean get () ;
        }
        property DateTime EndDate 
        {
		    virtual DateTime get () ;
        }
        property Boolean EndOfMonth 
        {
		    virtual Boolean get () ;
        }
		virtual Boolean IsRegular (UInt64 i) ;
		virtual DateTime NextDate (DateTime refDate) ;
		virtual DateTime PreviousDate (DateTime refDate) ;
        property QL::Times::DateGeneration::RuleEnum Rule 
        {
		    virtual QL::Times::DateGeneration::RuleEnum get () ;
        }
        property UInt64 Size 
        {
		    virtual UInt64 get () ;
        }
        property DateTime StartDate 
        {
		    virtual DateTime get () ;
        }
        property Cephei::QL::Times::IPeriod^ Tenor 
        {
		    virtual Cephei::QL::Times::IPeriod^ get () ;
        }
        property QL::Times::BusinessDayConventionEnum TerminationDateBusinessDayConvention 
        {
		    virtual QL::Times::BusinessDayConventionEnum get () ;
        }
		virtual Cephei::QL::Times::ISchedule^ Until (DateTime truncationDate) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Times::ISchedule^>::typeid)]
	[FactoryFor(Cephei::QL::Times::ISchedule::typeid)]
	[FactoryFor(Cephei::QL::Times::ISchedule_Factory::typeid)]
	public ref class CSchedule_Factory sealed : public ISchedule_Factory
	{
	public:
        virtual ISchedule^ Create ();
        virtual ISchedule^ Create (Cephei::Core::IVector<DateTime>^ dates, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::ICalendar^>^ calendar, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ convention);
        virtual ISchedule^ Create (DateTime effectiveDate, DateTime terminationDate, Cephei::QL::Times::IPeriod^ tenor, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, QL::Times::BusinessDayConventionEnum terminationDateConvention, QL::Times::DateGeneration::RuleEnum rule, Boolean endOfMonth, Microsoft::FSharp::Core::FSharpOption<DateTime>^ firstDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ nextToLastDate);
    };
   
/*Cephei*/ } /*QL*/ } /*Times */}
